<template>
  <el-card>
    <template #header>订单编辑</template>
    <el-form
      :model="info"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      size="mini"
    >
      <el-form-item label="订单id" prop="id">
        <el-input v-model="info.id"></el-input>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="info.orderNo"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="info.phone"></el-input>
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input v-model="info.consignee"></el-input>
      </el-form-item>
      <el-form-item label="送货地址" prop="deliverAddress">
        <el-input v-model="info.deliverAddress"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="info.remarks"></el-input>
      </el-form-item>
      <el-form-item label="订单金额" prop="orderAmount">
        <el-input v-model="info.orderAmount"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="info.orderState" placeholder="订单状态">
          <el-option label="已受理" value="已受理"></el-option>
          <el-option label="派送中" value="派送中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认修改</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { detailOrders, editOrders } from '../../api/order'
export default {
  data() {
    return {
      //数据对象
      info: {
        id: '',
        orderNo: '',
        phone: '',
        consignee: '',
        deliverAddress: '',
        remarks: '',
        orderAmount: '',
        orderState: '',
        orderTime: '',
        deliveryTime: ''
      },
      //规则对象
      rules: {
        id: [
          { required: true, message: '请输入商品id', trigger: 'blur' },
          { min: 0, max: 15, message: '长度在 0 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.detailOrder()
  },
  methods: {
    async detailOrder() {
      const { data } = await detailOrders({
        id: this.$route.params.id
      })
      console.log(data)
      this.info = data
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 验证成功
          const { code, msg } = await editOrders({ ...this.info })
          // console.log(data)
          if (code === 0) {
            this.$message.success(msg)
            setTimeout(() => {
              this.$router.push('/order/list')
            }, 1000)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
